<template>
  <div class="layout-main-container">
    <Myheader v-if="getIsShow()" ref="header"/>

    <div
      class="menu-box"
      v-show="show"
    >
      <el-tabs
        class="menu"
        :value="mainMenu"
        @tab-click="onClickSkip"
      >
        <template v-for="(item, index) in options">
          <el-tab-pane
            v-if="item.show"
            :key="index"
            :label="item.title"
            :name="item.path"
          ></el-tab-pane>
        </template>
      </el-tabs>

      <div
        class="menu-right-box"
        v-if="options.length"
      >
        <div class="button-box">
          <el-button
            class="add-button"
            v-if="current.rightButton.add"
            type="primary"
            size="mini"
            round
            @click="onClickAdd"
          > 新增 </el-button>
          <template v-if="current.rightButtons && current.rightButtons.length" v-for="(item, index) of current.rightButtons">
            <el-button
              v-if="(item.code && $verifyPerms(item.code)) || !item.code"
              :key="index"
              :icon="item.icon"
              :type="item.type"
              size="mini"
              round
              @click="onClickRightButton(item.methodName)"
            >
              {{ item.name }}
            </el-button>
          </template>
        </div>
      </div>
    </div>
    <div
      class="main"
      :class="{ 'main-no-tab': !show }"
    >
      <div class="container">
        <keep-alive v-if="$route.meta.keepAlive">
          <router-view ref="child"></router-view>
        </keep-alive>
        <router-view
          v-else
          ref="child"
        ></router-view>
      </div>
    </div>
  </div>
</template>

<script>
import Myheader from './header'
export default {
  name: 'mainContainer',
   components: {
    Myheader
  },
  data() {
    return {
      containerShow: false,
      callComming: false,
    };
  },
  props: {
    options: {
      type: Array,
      default() {
        return [];
      },
      required: true,
    },
    borderBottom: {
      type: Boolean,
      default() {
        return true;
      },
      required: false,
    },
  },
  computed: {
    show() {
      return this.options.filter(item => item.path.startsWith(this.$route.path)).length > 0;
    },
    current() {
      return this.options.find(item => item.path.startsWith(this.$route.path));
    },
    mainMenu() {
      return this.current ? this.current.path : '';
    },
  },
  methods: {
    getIsShow() {
      return top == self
    },
    onClickAdd() {
      if (this.$refs.child.onClickMenuRightButtonAdd) {
        this.$refs.child.onClickMenuRightButtonAdd();
      } else {
        this.$notification({
          type: 'error',
          title: '函数调用错误',
          message: '页面未定义onClickAdd事件处理函数',
        });
      }
    },
    onClickSearch(value) {
      if (this.$refs.child.onClickMenuRightButtonSearch) {
        this.$refs.child.onClickMenuRightButtonSearch(value);
      } else {
        this.$notification({
          type: 'error',
          title: '函数调用错误',
          message: '页面未定义onClickSearch事件处理函数',
        });
      }
    },
    onClickRightButton(methodName) {
      if (this.$refs.child[methodName]) {
        this.$refs.child[methodName]();
      } else {
        this.$notification({
          type: 'error',
          title: '函数调用错误',
          message: `页面未定义${methodName}事件处理函数`,
        });
      }
    },
    onClickAdd() {
      if (this.$refs.child.onClickMenuRightButtonAdd) {
        this.$refs.child.onClickMenuRightButtonAdd();
      } else {
        this.$notification({
          type: 'error',
          title: '函数调用错误',
          message: '页面未定义事件处理函数',
        });
      }
    },
    async onClickSkip(item) {
      this.$router.replace({
        path: item.name,
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.layout-main-container {
  width: 100%;
  height: 100vh;
  display: flex;
  flex-direction: column;
  
}

::v-deep .menu-box {
  position: sticky;
  z-index: 100;
  display: flex;
  align-items: center;
  height: 44px;
  margin: 0 20px;
  margin-top: 15px;
  background: #fff;

  .menu {
    flex: 1;
    border-bottom: unset !important;
    height: 44px;
    box-sizing: border-box;
    // box-shadow: 10px 0 0 #ffffff, -10px 0 0 #ffffff, 0 2px 5px #00000020;
    overflow: hidden;
    .el-menu-item {
      border-bottom: unset !important;
      position: relative;
      height: 44px;
      line-height: 44px;
      color: $color-999999;

      &:hover,
      &:focus {
        color: $primary-color;
      }
    }

    .is-active {
      position: relative;
      color: $primary-color;
      &:before {
        position: absolute;
        content: '';
        left: 50%;
        transform: translateX(-50%);
        bottom: 0;
        width: 20px;
        // height: 4px;
        background: $primary-color;
        border-radius: 2px;
      }
    }
  }

  .menu-right-box {
    display: flex;
    align-items: center;
    height: 44px;
    .button-box {
      .el-button {
        margin-left: 10px;
      }

      .is-round {
        padding: 10px 16px;
        border-radius: 17px;
        min-width: 80px;
      }
    }
  }
}

.show-border-bottom {
  border-bottom: 1px solid #e5e5e5;
}
.main {
  position: relative;
  width: 100%;
  flex: 1;
  box-sizing: border-box;
  overflow-y: auto;
  margin-top: 20px;
}
</style>
